﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace SCCN.Utility
{
    /// <summary>
    /// 生成静态Html文件帮助类
    /// </summary>
    public class CreateHTMLHelper
    {
        /// <summary>
        /// 根据字典键值对替换指定内容的标签
        /// </summary>
        /// <param name="content">文本内容</param>
        /// <param name="values">要替换的值</param>
        public static string ReplaceTag(Dictionary<string, string> values, string content)
        {
            foreach (KeyValuePair<string, string> item in values)
            {
                content = content.Replace(item.Key, item.Value);
            }
            return content;
        }

        /// <summary>
        /// 生成无bom格式的文件（仅限UTF-8编码使用）
        /// </summary>
        /// <param name="path">生成路径</param>
        /// <param name="content">文件内容</param>
        public static void CreateFileNoBom(string path, string content)
        {
            StreamWriter streamwriter = null;
            UTF8Encoding utf8 = new UTF8Encoding(false);
            try
            {
                using (streamwriter = new StreamWriter(path, false, utf8))
                {
                    streamwriter.WriteLine(content);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 生成指定文件
        /// </summary>
        /// <param name="path">要生成的文件路径</param>
        /// <param name="code">文件编码格式</param>
        /// <param name="content">文件内容</param>
        public static void CreateFile(string path, Encoding code, string content)
        {
            StreamWriter streamWriter = null;
            try
            {
                using (streamWriter = new StreamWriter(path, false, code))
                {
                    streamWriter.WriteLine(content);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 生成指定文件
        /// </summary>
        public static void CreateFile(string path, Encoding code, string content, bool isAppend)
        {
            StreamWriter streamWriter = null;
            try
            {
                using (streamWriter = new StreamWriter(path, isAppend, code))
                {
                    streamWriter.WriteLine(content);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 读取指定文件
        /// </summary>
        /// <param name="path">要读取的文件路径</param>
        /// <param name="code">文件编码格式</param>        
        public static string ReaderFileContent(string path, Encoding code)
        {
            if (File.Exists(path))
            {
                try
                {
                    using (StreamReader streamReader = new StreamReader(path, code))
                    {
                        return streamReader.ReadToEnd();
                    }
                }
                catch (IOException ioex)
                {
                    return string.Empty;
                    throw ioex;
                }
                catch (Exception ex)
                {
                    return string.Empty;
                    throw ex;
                }
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
